package com.wechat.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wechat.service.WeChatService;

import basic.base.web.BaseController;
import basic.commons.util.SHA1Util;


@Controller
@RequestMapping(value = "/wechat/commons")
public class WeChatCommonsController extends BaseController {
	private Logger logger = Logger.getLogger(WeChatCommonsController.class);

	@Autowired
	WeChatService service;

	@RequestMapping("/getSignature")
	@ResponseBody
	public Map<String,String> initUpdate(String url) {
		Map<String,String> resultMap=new HashMap<String,String>();
		String token = service.getAccessToken();
		String ticket = service.getTicket(token);
		String noncestr = UUID.randomUUID().toString().replace("-", "").substring(0, 16);//随机字符串
		String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳  
		String str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;
		String signature =SHA1Util.SHA1(str);  
		
		resultMap.put("noncestr", noncestr);
		resultMap.put("timestamp", timestamp);
		resultMap.put("signature", signature);
		System.out.println("token:"+token);
		System.out.println("jsapi_ticket:"+ticket);
		System.out.println("===noncestr:"+noncestr+"======timestamp:"+timestamp+"========signature:"+signature+"========");
		
		return resultMap;
	}

}
